﻿using System.Collections.Generic;
using Splendor_Server.script.logic.data;
using Splendor_Server.script.net;

namespace Splendor_Server.script.logic.player
{
    public class PlayerData
    {
        //胜利数
        public int win = 0;

        //总场数
        public int tot = 0;

        //好友列表 TODO
        public List<string> friendList = new List<string>();
    }

    public class PlayerGameData
    {
        public int point;
        public int[] gemstones;
        public int[] bonus;
        public int extras;

        public void AddGemstone(int type)
        {
            gemstones[type]++;
        }

        public void SubGemstone(int type, int num)
        {
            gemstones[type] -= num;
        }

        public void AddPoint(int num)
        {
            point += num;
        }

        public void AddExtra(int eId)
        {
            point += NobleData.data[eId].point;
            extras++;
        }

        public void AddBonus(int type)
        {
            bonus[type]++;
        }

        public void PurchaseMine(int level, int mId)
        {
            MineData.MineInfo mineInfo = MineData.data[level][mId];
            AddPoint(mineInfo.point);
            AddBonus((int) mineInfo.mineType);
            foreach (GameData.GemstoneType gemstoneType in mineInfo.needGemstones.Keys)
            {
                if (gemstones[(int) gemstoneType] >= mineInfo.needGemstones[gemstoneType])
                {
                    gemstones[(int) gemstoneType] -= mineInfo.needGemstones[gemstoneType];
                }
                else
                {
                    int useGold = mineInfo.needGemstones[gemstoneType] - gemstones[(int) gemstoneType];
                    gemstones[(int) gemstoneType] = 0;
                    gemstones[(int) GameData.GemstoneType.Gold] -= useGold;
                }
            }
        }
    }

    public class Player
    {
        private string id;

        public ClientState client;

        //房间Id
        private int _roomId = -1;

        private bool _isOwner;

        private bool _isPrepared;

        public PlayerData data;

        public PlayerGameData gameData;

        public Player(ClientState client, string id, PlayerData data)
        {
            this.client = client;
            this.id = id;
            this.data = data;
        }

        //向该玩家发送消息
        public void Send(MsgBase msgBase)
        {
            NetManager.Send(client, msgBase);
        }

        public PlayerInfo ToPlayerInfo()
        {
            return new PlayerInfo
            {
                pId = id,
                win = data.win,
                tot = data.tot,
                isOwner = _isOwner ? 1 : 0,
                isPrepared = _isPrepared ? 1 : 0
            };
        }

        public string GetPlayerId()
        {
            return id;
        }

        public int GetRoomId()
        {
            return _roomId;
        }

        public void EnterRoom(int rId)
        {
            _roomId = rId;
        }

        public void LeaveRoom()
        {
            _roomId = -1;
            _isOwner = false;
            _isPrepared = false;
        }

        public bool IsInRoom()
        {
            return _roomId >= 0;
        }

        public void StartBattle()
        {
            gameData = new PlayerGameData()
            {
                point = 0,
                gemstones = new int[6],
                bonus = new int[6],
                extras = 0
            };
            _isPrepared = false;
        }

        public void SetOwner()
        {
            _isOwner = true;
            _isPrepared = true;
        }

        public void SetPrepare(bool value)
        {
            _isPrepared = value;
        }

        public bool IsOwner()
        {
            return _isOwner;
        }

        public bool IsPrepared()
        {
            return _isPrepared;
        }
    }
}